package com.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Servlet Filter implementation class loginfilter2
 */
@WebFilter("/*")
public class LoginFilter implements Filter {
	
	private static List<String> urls = new ArrayList<>();
    //静态代码块中向集合中存放所有可以放行的请求或网页地址（不用账号密码即可访问）
    static {
        urls.add("/index");
    }

    /**
     * Default constructor. 
     */
    public LoginFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		HttpServletRequest req=(HttpServletRequest)request;
		HttpServletResponse res=(HttpServletResponse)response; 
		String urlPattern=req.getServletPath();
		String uri=req.getRequestURI();
		//System.out.println("url:"+urlPattern);
		//System.out.println("uri:"+uri);
		for (String url : urls) {
            if(urlPattern.contains(url) || uri.equals("/demo/") || uri.contains("TeacherLogin") || uri.contains("AdminLogin") || uri.contains("StudentLogin")) {
        //        System.out.println("reaource do chain...");
            	//System.out.println("url1:"+urlPattern);
        		//System.out.println("uri1:"+uri);
                chain.doFilter(request, response);
                //防止重复响应
                return;
            }
        }
		System.out.println(req.getSession().getAttribute("stu_id"));
		System.out.println("url:"+urlPattern);
		System.out.println("uri:"+uri);
		if((urlPattern.contains("student")&&req.getSession().getAttribute("stu_id")==null) || (urlPattern.contains("admin")&&req.getSession().getAttribute("admin")==null) || (urlPattern.contains("admin")&&req.getSession().getAttribute("admin")==null) || (urlPattern.contains("teacher")&&req.getSession().getAttribute("teacher")==null)) {
			System.out.println("student:"+req.getSession().getAttribute("stu_id"));
			res.sendRedirect("../index.jsp");
        }else { 
            //放行
        	//System.out.println("url2:"+urlPattern);
    		//System.out.println("uri2:"+uri);
            chain.doFilter(request, response);
        }
//		if((urlPattern.contains("admin_main")&&req.getSession().getAttribute("admin")!=null) || (urlPattern.contains("teacher_main")&&req.getSession().getAttribute("teacher")!=null)) {
//			chain.doFilter(request, response);
//		}
//		else {
//			res.sendRedirect("../index.jsp");
//		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
